package com.engc.common.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

@Controller
@RequestMapping("/systemDict")
public class SysDict {
	private BaseService baseService;

	@RequestMapping("/view")
	public String view() {
		return "system/sysdict/SysDict";
	}

	@RequestMapping("/getDictTypes")
	@ResponseBody
	public List<Map<String, Object>> getDictType(String key) {
		List<Map<String, Object>> dicttypes = null;
		if(null == key || "".equals(key)){
			dicttypes = baseService.Query("select distinct type from sys_dict");
		}else{
			dicttypes = baseService.Query("select distinct type from sys_dict where type like '"+key+"%'");
		}
		
		for (Map<String, Object> type : dicttypes) {
			type.put("id", type.get("type"));
			type.put("text", type.get("type"));
		}
		return dicttypes;
	}

	@RequestMapping("/insertDict")
	@ResponseBody
	public Map<String, Object> insertDict(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		record.put("id", baseService.getSeqValue("SEQ_SYS_DICT"));
		record.remove("dictype");
		try {
			baseService.Insert("sys_dict", record);
			return BaseUtils.getSuccessMessage("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("添加失败，系统发生异常");
		}
	}

	@RequestMapping("/updateDict")
	@ResponseBody
	public Map<String, Object> updateDict(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getUpdateParam(request, "id");
		try {
			baseService.Update("sys_dict", record, "id=?", request.getParameter("id"));
			return BaseUtils.getSuccessMessage("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			return BaseUtils.getErrorMessage("更新失败");
		}
	}

	@RequestMapping("/delDict")
	@ResponseBody
	public Map<String, Object> DelDict(HttpServletRequest request,Long id) {
		try{
			
			baseService.Delete("delete from sys_dict where id=?", new Object[]{id});
			return BaseUtils.getSuccessMessage("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return BaseUtils.getErrorMessage("删除失败");
		}
		
		
	}

	@Resource
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
